#!/usr/bin/python


def build_coreimg():
    print_info('coreimg', 'Building core image')
    
    # Files
    src_files = [ \
        bin_dir + 'tdlrhal.bin',
        bin_dir + 'tdlrkrnl.bin',
        bin_dir + 'tdlrdrv.bin',
        bin_dir + 'tdlrsys.bin',
        bin_dir + 'tdlrshell.bin',
        src_dir + 'init/init.py',
    ]
    target_name = bin_dir + 'tdlrcore.img'
    
    # Check if we need to rebuild the img
    regenerate = False
    for f in src_files:
        if need_build(f, target_name):
            regenerate = True
            break
        
    if not regenerate:
        return
    
    # Compose the cmd
    cmd = tools_bin_dir + 'coreimg ' + target_name
    for f in src_files:
        cmd += ' ' + f
        
    # Execute the cmd
    code = exec_cmd(cmd)
    assert(code == 0)
    
    # Update record
    record_update(target_name)


def build_main():
    # Build kernel
    print_info('kernel', 'Building kernel')
    
    kernel_ext_dep = []
    kernel_ext_flags = {}
    for d in arch_dirs:
        kernel_ld = d + 'kernel/kernel.ld'
        if os.path.exists(kernel_ld):
            kernel_ext_dep.append(kernel_ld)
            kernel_ext_flags['ld/script'] = kernel_ld
            break
    else:
        kernel_ext_flags['ld/ext'] = '-Ttext 0xFFF01000'
    
    kernel_files = get_all_files(src_dir + 'kernel/', [ '.c' ])
    for d in arch_dirs:
        kernel_files += get_all_files(d + 'kernel/', [ '.c', '.asm', '.s', '.S' ])
    
    build_files(
        kernel_files,
        bin_dir + 'tdlrkrnl.bin',
        ext_dep = kernel_ext_dep,
        ext_flags = kernel_ext_flags,
    )
    
    # Build klibc
    print_info('klibc', 'Building klibc')
    
    klib_files = get_all_files(src_dir + 'klibc/', [ '.c' ])
    for d in arch_dirs:
        klib_files += get_all_files(d + 'klibc/', [ '.c', '.asm', '.s', '.S' ])
    
    lib_files(
        klib_files,
        bin_dir + 'tdlrklibc.a'
    )
    
    # External flags for generic processes
    user_ext_dep = []
    user_ext_flags = {}
    
    for d in arch_dirs:
        user_ld = d + 'klibc/klibc.ld'
        if os.path.exists(user_ld):
            user_ext_dep.append(user_ld)
            user_ext_flags['ld/script'] = user_ld
            break
    else:
        user_ext_flags['ld/ext'] = '-e _start'
    
    # Build driver
    print_info('driver', 'Building driver')
    build_dir(
        src_dir + 'driver/', [ '.c' ],
        bin_dir + 'tdlrdrv.bin',
        ext_libs = [ bin_dir + 'tdlrklibc.a' ],
        ext_dep = user_ext_dep,
        ext_flags = user_ext_flags,
    )

    # Build system
    print_info('system', 'Building system')
    build_dir(
        src_dir + 'system/', [ '.c' ],
        bin_dir + 'tdlrsys.bin',
        ext_libs = [ bin_dir + 'tdlrklibc.a' ],
        ext_dep = user_ext_dep,
        ext_flags = user_ext_flags,
    )
    
    # Build shell
    print_info('shell', 'Building shell')
    build_dir(
        src_dir + 'shell/', [ '.c' ],
        bin_dir + 'tdlrshell.bin',
        ext_libs = [ bin_dir + 'tdlrklibc.a' ],
        ext_dep = user_ext_dep,
        ext_flags = user_ext_flags,
    )


# Setup callback functions
arch_funcs['build_coreimg'] = build_coreimg
arch_funcs['build_main'] = build_main
